package com.hqyj.hrms.modules.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.hrms.modules.account.dao.UserMapper;
import com.hqyj.hrms.modules.account.entity.User;
import com.hqyj.hrms.modules.account.service.UserRedisService;
import com.hqyj.hrms.utils.MyEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class UserRedisServiceImpl implements UserRedisService {
    @Autowired(required = false)
    UserMapper userMapper;

    //注入redis
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    //注入邮件发送工具类
    @Autowired
    MyEmail myEmail;

    @Override
    public HashMap<String, Object> selectRedis(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //假设登录用户名
        String userName = "admin";
        //定义当前页集合的建名
        String key01 = "user:list:" + user.getPage() + ":" + user.getRow() + ":" + userName;
        //定义当前页总条数的建名
        String key02 = "user:list:" + user.getPage() + ":" + user.getRow() + ":" + userName;
        //定义当前页总页数的建名
        String key03 = "user:list:" + user.getPage() + ":" + user.getRow() + ":" + userName;

        //判断缓存是否有数据
        Object obj = redisTemplate.opsForValue().get(key01);
        if (obj == null) {
            //第一步：创建分页对象
            Page<User> userInfoPage = new Page<>(user.getPage(), user.getRow());
            //创建查询条件对象
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(
                    user.getUserName() != null && !user.getUserName().equals(""),
                    "user_name",
                    user.getUserName());

            //第二步：查询分页，获取分页数据集
            Page<User> pageList = userMapper.selectPage(userInfoPage, queryWrapper);
            //获取当前页的集合
            map.put("info", pageList.getRecords());
            //获取总条数
            map.put("total", pageList.getTotal());
            //获取总页数
            map.put("totalPage", pageList.getPages());
            //存入缓存中
            redisTemplate.opsForValue().set(key01, pageList.getRecords(), 15, TimeUnit.MINUTES);
            redisTemplate.opsForValue().set(key02, pageList.getTotal(), 15, TimeUnit.MINUTES);
            redisTemplate.opsForValue().set(key03, pageList.getPages(), 15, TimeUnit.MINUTES);

            //记录
        } else {
            //从缓存中取数据
            List<User> list = (List<User>) redisTemplate.opsForValue().get(key01);
            String total = redisTemplate.opsForValue().get(key02) + "";
            String totalPage = redisTemplate.opsForValue().get(key03) + "";
            //获取当前页的集合
            map.put("info", list);
            //获取总条数
            map.put("total", total);
            //获取总页数
            map.put("totalPage", totalPage);

        }
        return map;
    }

    @Override
    public HashMap<String, Object> add(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = userMapper.updateById(user);
        if (num > 0) {
            map.put("info", "保存成功");
            //更新缓存
            Set<Object> objectSet = redisTemplate.opsForSet().members("userinfo:totalKey");
            //迭代遍历
            Iterator<Object> iterator = objectSet.iterator();
            while (iterator.hasNext()) {
                //删除建名
                redisTemplate.delete(iterator.next() + "");
            }
        } else {
            map.put("info", "新增失败");
        }
        return map;
    }

    //redis版本的发送验证码
    @Override
    public HashMap<String, Object> emailRedis(String email) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //生成4位随机数
        Random rd = new Random();
        int code = rd.nextInt(1000);
        map = myEmail.sendEmail(email, "你的验证码是：" + code, "xxxx公司的验证码");
        if (map.get("info").equals("发送成功")) {
            //存入redis
            redisTemplate.opsForValue().set(email, code + "", 3, TimeUnit.MINUTES);
        }
        return map;
    }

    //redis版本的邮件登录
    @Override
    public HashMap<String, Object> LoginEmailRedis(String code, String email) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        String code1 = redisTemplate.opsForValue().get(email) + "";
        if (code.equals(code1)) {
            map.put("info", "邮箱登录成功");
            //map.put("info", "验证码输入不正确");
        } else {
            map.put("info", "验证码输入不正确");

        }

        return map;
    }
}
